package dynamicProgramming;

public class TestWeightBagProblem {

    public static void test2_weight_bag_problem() {
        int[] weight = {1, 3, 4};
        int[] value = {15, 20, 30};
        int bagWeight = 4;
        int[] dp = new int[bagWeight + 1];
        for (int i = 0; i < weight.length; i++) {
            for (int j = bagWeight; j >= weight[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
            }
        }
        System.out.println(dp[bagWeight]);
    }


    public static void test_weight_bag_problem() {
        int[] weight = {1, 3, 4};
        int[] value = {15, 20, 30};
        int bagWeight = 4;
        int[][] dp = new int[weight.length][bagWeight + 1];
        for (int i = weight[0]; i <= bagWeight; i++)
            dp[0][i] = value[0];
        for (int i = 1; i < weight.length; i++)
            for (int j = 1; j <= bagWeight; j++)
                if (j < weight[i]) dp[i][j] = dp[i - 1][j];
                else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
        System.out.println(dp[weight.length - 1][bagWeight]);
    }

    public static void main(String[] args) {
        test2_weight_bag_problem();
    }
}
